import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class GamesHandler extends HttpServlet {
	public int gameId;

	public static ArrayList<Game> games = new ArrayList<Game>();

	public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException,
	IOException {
		try{
			HttpSession session = req.getSession();
			String name="";
			name = session.getAttribute("username").toString();		
			String comand = req.getParameter("comand");

			if(comand.equals("create")){
				gameId++;
				System.out.print("GAME SIZE: "+games.size());
				createNewGame(name,gameId);
				session.setAttribute("gameID", gameId);
			}
			else if(comand.equals("remove")){

				int id = (Integer) session.getAttribute("gameID");

				for (int i = games.size()-1; i>-1; i--) {
					if(games.get(i).getId()==id){	
						games.remove(games.get(i));

						break;
					}
				}

			}

			else{	
				for (int i = games.size()-1; i>-1; i--) {
					if(games.get(i).getPl1().equals(comand)){
						if(games.get(i).getPl2()==null){
							addPlayerToGame(games.get(i), name);
							session.setAttribute("gameID", games.get(i).getId());
						}
						else{
							PrintWriter out = res.getWriter();
							out.print("error");
							out.flush();
						}
						break;
					}
				}						
			}
		}catch (NullPointerException e) {
			
			try {
				finalize();
			} catch (Throwable e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

		}
	}

	public void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException,
	IOException {
		response.setContentType("application/json");
		PrintWriter out = response.getWriter();

		String temp="";
		String temp1="";
		String beginHTHL="<h1>Already going games</h1><div id='goingGames' class='myBox'>";
		String centerHTML="</div><h1>Waiting for opponent</h1><div id='waitinGames' class='myBox'>";
		String endHTHL="</div>";

		for (int i = 0; i < games.size(); i++) {
			if(games.get(i).getPl2()==null){
				temp+=games.get(i).getPl1()+"<img id='"+games.get(i).getPl1()+"' class='info' src='images/info.png' alt='User statistics'></img><button id='"+games.get(i).getPl1()+"' class='join'>Join game</button></br>";
			}
			else{
				temp1+=games.get(i).getPl1()+"<img id='"+games.get(i).getPl1()+"' class='info' src='images/info.png' alt='User statistics'></img> --- VS --- "+games.get(i).getPl2()+"<img id="+games.get(i).getPl2()+" class='info' src='images/info.png' alt='User statistics'></img></br>";
			}

		}
		out.print(beginHTHL+temp1+centerHTML+temp+endHTHL);
		out.flush();
	}

	public void createNewGame(String name, int id){
		Game g1=new Game(name, null, id, null, null, null, null, false, false, false);
		games.add(g1);
	}
	public void addPlayerToGame(Game game, String name){
		game.setPl2(name);
	}

}


